<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/plugins/element-ui/index.css"/>
    <style>
        .demo-ruleForm {
            width: 500px;
            margin-top: 20px;
        }
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 120px;
            height: 120px;
            line-height: 120px;
            text-align: center;
        }
        .avatar {
            width: 120px;
            height: 120px;
            display: block;
        }
    </style>
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm">
            <el-form-item
                    label="缩略图:"
                    prop="region"
                    class="uploadImg">
                <el-upload
                        class="avatar-uploader"
                        action="/common/upload"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :on-change="onChange"
                        ref="upload">
                    <img v-if="imgUrl" :src="imgUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item
                    label="商品名:"
                    prop="productName">
                <el-input v-model="ruleForm.productName" placeholder="请输入商品名"></el-input>
            </el-form-item>
            <el-form-item
                    label="分类:"
                    prop="categoryId">
                <el-select v-model="level1" placeholder="请选择一级分类" class="select">
                    <el-option
                            v-for="item in categories1"
                            :key="item.categoryId"
                            :label="item.categoryName"
                            :value="item.categoryId">
                    </el-option>
                </el-select>
                <el-select v-model="ruleForm.categoryId" placeholder="请选择二级分类" class="select">
                    <el-option
                            v-for="item in categories2"
                            :key="item.categoryId"
                            :label="item.categoryName"
                            :value="item.categoryId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                    label="现价:"
                    prop="reprice">
                <el-input v-model="ruleForm.reprice" placeholder="请输入现价"></el-input>
            </el-form-item>
            <el-form-item
                    label="原价:"
                    prop="price">
                <el-input v-model="ruleForm.price" placeholder="请输入原价"></el-input>
            </el-form-item>
            <el-form-item
                    label="详情:"
                    prop="details"
                    class="uploadImg">
                <el-upload
                        class="avatar-uploader"
                        action="/common/upload"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess1"
                        :on-change="onChange"
                        ref="upload">
                    <img v-if="detailsUrl" :src="detailsUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item
                    label="库存:"
                    prop="stock">
                <el-input v-model="ruleForm.stock" placeholder="请输入库存数量"></el-input>
            </el-form-item>
            <el-form-item
                    label="生产地:"
                    prop="address">
                <el-input v-model="ruleForm.address" placeholder="请输入生产地"></el-input>
            </el-form-item>
            <el-form-item
                    label="保质期:"
                    prop="life">
                <el-input v-model="ruleForm.life" placeholder="请输入保质期"></el-input>
            </el-form-item>

            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm', false)">
                        保存
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>

<script src="/plugins/vue/vue.js"></script>
<script src="/plugins/element-ui/index.js"></script>
<script src="/plugins/axios/axios-0.18.0.js"></script>
<script src="/js/index.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                id: '',
                actionType: '',
                ruleForm: {},
                imgUrl: '',
                detailsUrl: '',
                categories1: [],
                categories2: [],
                level1: '',
            }
        },
        watch: {
            level1(newVal) {
                this.getClassify(newVal)
            }
        },
        created() {
            this.getClassify(0)
            this.id = requestUrlParam('id')  //获取url地址上面的参数
            this.actionType = this.id ? 'edit' : 'add'
            if (this.id) {
                this.init()
            }
        },
        methods: {
            init() {
                axios.get('/product/' + this.id).then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        this.ruleForm = info.data
                        this.imgUrl = `/common/download?fileName=${this.ruleForm.thumbnail}`
                        this.detailsUrl = `/common/download?fileName=${this.ruleForm.details}`
                        this.getClassify(info.data.categoryTopId)
                    } else {
                        this.$message.error(info.msg || '操作失败')
                    }
                })
            },
            getClassify(level) {
                axios.get('/category/query?categoryTopId=' + level).then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        if (level == 0)
                            this.categories1 = info.data.records
                        else
                            this.categories2 = info.data.records
                    } else {
                        this.$message.error(info.msg || '操作失败')
                    }
                })
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            axios.post('/product', this.ruleForm).then(res => {
                                let info = res.data
                                if (info.code === 1) {
                                    this.$message.success('用户添加成功！')
                                    this.ruleForm = {}
                                    this.goBack()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            axios.put('/product', this.ruleForm).then(res => {
                                let info = res.data
                                if (info.code === 1) {
                                    this.$message.success('用户修改成功！')
                                    this.goBack()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            handleAvatarSuccess (response, file, fileList) {
                this.imgUrl = `/common/download?fileName=${response.data}`
                this.ruleForm.thumbnail = response.data
            },
            handleAvatarSuccess1 (response, file, fileList) {
                this.detailsUrl = `/common/download?fileName=${response.data}`
                this.ruleForm.details = response.data
            },
            onChange (file) {
                if(file) {
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 1
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 1MB!')
                        return false
                    }
                    return file
                }
            },
            goBack() {
                window.location.href = '/page/product/list.html'
            },
        }
    })
</script>
</body>
</html>